package com.map;

import java.awt.GridBagConstraints;
import java.awt.Point;
import java.awt.event.MouseEvent;
import com.map.frame.ProgressBarDialog;
import com.map.mwing.BigMapLabel;
import com.map.tool.InstancePool;
import com.map.tool.MapProcessor;

public class MapPanel extends javax.swing.JPanel
{                  
    private javax.swing.JButton addButton;
    private javax.swing.JPanel adjustMapPanel;
    private static BigMapLabel bigMapLabel;
    private javax.swing.JButton downButton;
    private javax.swing.JButton leftButton;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JButton revertButton;//还原按钮
    private javax.swing.JButton rightButton;
    private javax.swing.JSlider slider;
    private javax.swing.JButton subButton;//缩小按钮
    private javax.swing.JButton upButton;//放大按钮
    private MapProcessor mapProcessor;
    private Point lastpos;
    private boolean isDrag = false;
    
    public MapPanel() 
    {
        initComponents();
    }

    public MapProcessor getMapProcessor() 
    {
        return mapProcessor;
    }


    private void initComponents() 
    {
    	GridBagConstraints gridBagConstraints;
        leftPanel = new javax.swing.JPanel();
        bigMapLabel = new BigMapLabel();//显示图片的label
        adjustMapPanel = new javax.swing.JPanel();
        upButton = new javax.swing.JButton();
        downButton = new javax.swing.JButton();
        revertButton = new javax.swing.JButton();
        leftButton = new javax.swing.JButton();
        rightButton = new javax.swing.JButton();
        subButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        slider = new javax.swing.JSlider();
        
        setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        addComponentListener(new java.awt.event.ComponentAdapter() 
        {
            public void componentResized(java.awt.event.ComponentEvent evt)
            {
                formComponentResized(evt);
            }
        });
        
        setLayout(new java.awt.BorderLayout());
        leftPanel.setLayout(new java.awt.BorderLayout());
        bigMapLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        bigMapLabel.addMouseListener(new java.awt.event.MouseAdapter() 
        {
        	public void mousePressed(MouseEvent e)
        	{
        		lastpos = e.getPoint();
        		isDrag = true;
        		
        	}
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bigMapLabelMouseClicked(evt);
            }
            public void mouseReleased(MouseEvent e) {
            	isDrag = false;
            	
        	}
        });
        
        bigMapLabel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() 
        {
        	
            public void mouseDragged(java.awt.event.MouseEvent evt)
            {
                bigMapLabelMouseMoved(evt);
            }
        });
        
        leftPanel.add(bigMapLabel, java.awt.BorderLayout.CENTER);
        adjustMapPanel.setLayout(new java.awt.GridBagLayout());
        upButton.setText("▲");
        upButton.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                upButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        adjustMapPanel.add(upButton, gridBagConstraints);

        downButton.setText("▼");
        downButton.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                downButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        adjustMapPanel.add(downButton, gridBagConstraints);

        revertButton.setText("■");
        revertButton.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                revertButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        adjustMapPanel.add(revertButton, gridBagConstraints);

        leftButton.setText("<");
        leftButton.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                leftButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 40, 0, 0);
        adjustMapPanel.add(leftButton, gridBagConstraints);

        rightButton.setText(">");
        rightButton.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                rightButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        adjustMapPanel.add(rightButton, gridBagConstraints);

        subButton.setText("-");
        subButton.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                subButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        adjustMapPanel.add(subButton, gridBagConstraints);

        addButton.setText("+");
        addButton.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                addButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        adjustMapPanel.add(addButton, gridBagConstraints);

       
        slider.setMaximum(300);
        slider.setMinimum(-300);
        slider.setMinorTickSpacing(25);
        slider.setPaintTicks(true);
        slider.setSnapToTicks(true);
        slider.setValue(150);
        
        slider.addChangeListener(new javax.swing.event.ChangeListener() 
        {
            public void stateChanged(javax.swing.event.ChangeEvent evt) 
            {
                sliderStateChanged(evt);
            }
        });
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        
        adjustMapPanel.add(slider, gridBagConstraints);
        leftPanel.add(adjustMapPanel, java.awt.BorderLayout.SOUTH);
        add(leftPanel, java.awt.BorderLayout.CENTER);
    }                   

	private void upButtonActionPerformed(java.awt.event.ActionEvent evt) 
	{              
	    bigMapLabel.setIcon(mapProcessor.moveOfVertical(-100));// 刷新图片显示区
	    //判断垂直移动按钮是否可用
	    if (!downButton.isEnabled()) 
	    {
	        downButton.setEnabled(true);
	    } 
	    else
	    {
	        if (mapProcessor.getShowCenterY() - mapProcessor.getCutMapHeight() / 2 < 2)
	        {
	            upButton.setEnabled(false);
	        }
	    }
	    
	}                                        

	private void addButtonActionPerformed(java.awt.event.ActionEvent evt) 
	{                                          
	
	    slider.setValue(slider.getValue() + 10);// 将缩放比例增加一个单位
	}                                         
	
	                                           
	
	private void subButtonActionPerformed(java.awt.event.ActionEvent evt) 
	{                                          
	
	    slider.setValue(slider.getValue() - 10);// 将缩放比例减小一个单位
	}                                         

	
	private void sliderStateChanged(javax.swing.event.ChangeEvent evt) 
	{     
		int v = slider.getValue();// 获得划块的值
    
		// 判断比例调整按钮是否可用
		if (v == slider.getMinimum()) 
		{
			// 调整为最小值
			subButton.setEnabled(false);// 调小按钮不可用
			addButton.setEnabled(true);// 设置调大按钮可用

		} 
		else if (v == slider.getMaximum()) 
		{
			// 调整为最大值
			addButton.setEnabled(false);// 调大按钮不可用
			subButton.setEnabled(true);// 设置调小按钮可用

		} 
		else
		{      
            subButton.setEnabled(true);// 设置调小按钮可用
            addButton.setEnabled(true);// 设置调大按钮可用
		}
		//刷新图片
		bigMapLabel.setIcon(mapProcessor.adjustScale(v));// 重绘图片显示区


		//判断水平移动按钮是否可用
		int w = mapProcessor.getCutMapWidth() / 2;
		if (mapProcessor.getShowCenterX() - w < 2 || mapProcessor.getMap().getWidth() - mapProcessor.getShowCenterX() - w < 2) 
		{
			if (mapProcessor.getShowCenterX() <= w) 
			{
				leftButton.setEnabled(false);
			} 
			else 	
			{
				rightButton.setEnabled(false);
			}
		}
		else 
		{
			if (!leftButton.isEnabled())
			{
				leftButton.setEnabled(true);
			}
			if (!rightButton.isEnabled()) 
			{
				rightButton.setEnabled(true);
			}
		}
		//判断垂直移动按钮是否可用
		int h = mapProcessor.getCutMapHeight() / 2;
		if (mapProcessor.getShowCenterY() - h < 2 || mapProcessor.getMap().getHeight() - mapProcessor.getShowCenterY() - h < 2) 
		{
			if (mapProcessor.getShowCenterY() <= h) 
			{
					upButton.setEnabled(false);
			}
			else 
			{
				downButton.setEnabled(false);
			}
		} 
		else 
		{
			if (!upButton.isEnabled())
			{
				upButton.setEnabled(true);
			}
			if (!downButton.isEnabled()) 
			{
				downButton.setEnabled(true);
			}
		}
	}                                   

                                              
	
	private void formComponentResized(java.awt.event.ComponentEvent evt)
	{                                      
	
	    if (mapProcessor == null) {
	      
	        new Thread() {
	            @Override
	            public void run() 
	            {
	            	
	            	ProgressBarDialog progressBarDialog = new ProgressBarDialog(null, false);
	            	progressBarDialog.setVisible(true);
	                mapProcessor = new MapProcessor(OpenFile.filePath.getText(), getBigMapLabel().getWidth(), getBigMapLabel().getHeight(), slider.getValue());
	                InstancePool.setMapProcessor(mapProcessor);
	                InstancePool.setScaleSlider(slider);
	                getBigMapLabel().setIcon(mapProcessor.adjustWindow(getBigMapLabel().getWidth(), getBigMapLabel().getHeight()));     
	                System.out.println(getBigMapLabel().getWidth()+","+getBigMapLabel().getHeight());
	                progressBarDialog.dispose();
	            }
	        }.start();
	       
	    } 
	    else 
	    {
	        getBigMapLabel().setIcon(mapProcessor.adjustWindow(getBigMapLabel().getWidth(), getBigMapLabel().getHeight()));
	       
	    }
	}                                     
	
	private void downButtonActionPerformed(java.awt.event.ActionEvent evt)
	{              
	    getBigMapLabel().setIcon(mapProcessor.moveOfVertical(100));// 刷新图片显示区
	
	    if (!upButton.isEnabled()) 
	    {
	        upButton.setEnabled(true);
	    }
	    else 
	    {
	        if (mapProcessor.getMap().getHeight() - mapProcessor.getShowCenterY() - mapProcessor.getCutMapHeight() / 2 < 2) 
	        {
	            downButton.setEnabled(false);
	        }
	    }
	   
	}                                          
	
	private void leftButtonActionPerformed(java.awt.event.ActionEvent evt)
	{                                           
	
	    getBigMapLabel().setIcon(mapProcessor.moveOfHorizontal(-100));// 刷新图片显示区
	    //判断水平移动按钮是否可用
	
	    if (!rightButton.isEnabled()) 
	    {
	        rightButton.setEnabled(true);
	    } 
	    else 
	    {
	        if (mapProcessor.getShowCenterX() - mapProcessor.getCutMapWidth() / 2 < 2) 
	        {
	            leftButton.setEnabled(false);
	        }
	    }
	   
	}                                          
	
	private void rightButtonActionPerformed(java.awt.event.ActionEvent evt) 
	{                                            
	  
	    getBigMapLabel().setIcon(mapProcessor.moveOfHorizontal(100));// 刷新图片显示区
	
	    if (!leftButton.isEnabled()) 
	    {
	        leftButton.setEnabled(true);
	    } 
	    else 
	    {
	        if (mapProcessor.getMap().getWidth() - mapProcessor.getShowCenterX() - mapProcessor.getCutMapWidth() / 2 < 2)
	        {
	            rightButton.setEnabled(false);
	        }
	    }
	   
	}                                           
	
	private void revertButtonActionPerformed(java.awt.event.ActionEvent evt)
	{                                             
	  
	    mapProcessor.revert();// 还原为默认的视觉中心
	    slider.setValue(-250);// 还原为默认的缩放比例
	    // 如果用于移动图片的按钮不可用，则将其设为可用
	    if (!upButton.isEnabled()) 
	    {
	        upButton.setEnabled(true);
	    }
	    if (!downButton.isEnabled()) 
	    {
	        downButton.setEnabled(true);
	    }
	    if (!leftButton.isEnabled())
	    {
	        leftButton.setEnabled(true);
	    }
	    if (!rightButton.isEnabled())
	    {
	        rightButton.setEnabled(true);
	    }
	}                                                                                   
	
	private void bigMapLabelMouseClicked(java.awt.event.MouseEvent evt) 
	{                                         
	// TODO add your handling code here:
	    if (evt.getButton() == MouseEvent.BUTTON3)
	    {// 是否由单击鼠标右键触发
	
	    }
	}           
	private void bigMapLabelMouseMoved(java.awt.event.MouseEvent e) 
	{                                       
		Point newpos = e.getPoint();
		System.out.println("move:"+isDrag);				
		getBigMapLabel().setIcon(mapProcessor.movePic(lastpos.x-newpos.x,lastpos.y-newpos.y));
		lastpos = newpos;
	}          
    public static BigMapLabel getBigMapLabel()
    {
        return bigMapLabel;
    }
}
